package hot100.h0911;

/**
 * @author 不知名帅哥
 * @version 1.0
 * @description 字符串压缩
 * @date 2024/9/11 22:33
 */
public class CompressString {
    public static void main(String[] args) {
        String s = "aabcccccaa";
        System.out.println(compressString(s));
    }

    public static String compressString(String S) {
        StringBuilder builder = new StringBuilder();
        int i = 0;
        while (i < S.length()) {
            char c = S.charAt(i);
            builder.append(c);
            int count = 0;
            while (i < S.length() && S.charAt(i) == c) {
                count++;
                i++;
            }
            if (count >= 1) {
                builder.append(count);
            }
        }
        if (builder.length() >= S.length()) {
            return S;
        }
        return builder.toString();
    }
}
